<?php

namespace app\models;

use app\components\Func;
use Yii;

/**
 * This is the model class for table "qun".
 *
 * @property int $id
 * @property string $title
 * @property float $lat
 * @property float $lng
 * @property int $ctime
 * @property int $pic
 * @property int $uid
 * @property int $state
 */
class Qun extends \yii\db\ActiveRecord
{
    //获取附近的对象
    static public function getNearObjs($lng,$lat,$distance){
        $range_arr = Func::countGpsRange($lng,$lat,$distance);
//        print_r($range_arr);exit;
        $query = self::find();
        $query->andWhere(['>','lng',$range_arr['left-top']['lng']])
            ->andWhere(['<','lng',$range_arr['right-bottom']['lng']])
            ->andWhere(['<','lat',$range_arr['left-top']['lat']])
            ->andWhere(['>','lat',$range_arr['right-bottom']['lat']]);
//        $objs_find->andWhere(['state'=>self::STATE_PASS]);
        $objs = $query->all();
        return $objs;
    }
    //关联
    public function getPicObj(){
        return $this->hasOne(UploadFile::className(),['id'=>'pic']);
    }
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'qun';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['title', 'lat', 'lng', 'ctime', 'pic', 'uid'], 'required'],
            [['lat', 'lng'], 'number'],
            [['ctime', 'pic', 'uid', 'state'], 'integer'],
            [['title'], 'string', 'max' => 255],
            [['title'], 'unique'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'title' => 'Title',
            'lat' => 'Lat',
            'lng' => 'Lng',
            'ctime' => 'Ctime',
            'pic' => 'Pic',
            'uid' => 'Uid',
            'state' => 'State',
        ];
    }
}
